package com.lxl.bt.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lxl.bt.Constant;
import com.lxl.bt.R;
import com.lxl.bt.activity.BaseFragment;
import com.lxl.bt.activity.WebviewActivity;
import com.lxl.bt.bean.A_Login;
import com.lxl.bt.bean.NewBean;
import com.lxl.bt.net.NetApi;
import com.lxl.bt.net.Result;
import com.lxl.bt.retorfit.NetCallback;
import com.lxl.bt.retorfit.RetrofitUtil;
import com.lxl.bt.util.ACache;
import com.lxl.bt.util.GlideUtils;
import com.lxl.bt.util.Prefs;
import com.lxl.bt.util.TimeUtil;
import com.yanzhenjie.sofia.Sofia;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Create by lvxinlong
 * on 发现
 * on 2021年4月9日22:10:42
 **/
public class FindFragment extends BaseFragment {


    A_Login a_login;
    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.txt_toolbar_title)
    TextView txtToolbarTitle;
    @BindView(R.id.rlBack)
    LinearLayout rlBack;
    @BindView(R.id.ivMessage)
    ImageView ivMessage;
    @BindView(R.id.tvMessageTag)
    TextView tvMessageTag;
    @BindView(R.id.rlRight)
    RelativeLayout rlRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rcv)
    RecyclerView rcv;
    private Unbinder unbinder;
    ACache aCache;
    BaseQuickAdapter<NewBean, BaseViewHolder> sysAdapter;

    public static FindFragment newInstance() {
        Bundle bundle = new Bundle();
        FindFragment fragment = new FindFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_find, container, false);
//        ((BaseActivity) getActivity()).setStatusBarFullTransparent();

        //返回一个Unbinder值（进行解绑），注意这里的this不能使用getActivity()
        unbinder = ButterKnife.bind(this, view);
        aCache = ACache.get(getActivity());
        a_login = (A_Login) aCache.getAsObject(Constant.LOGIN);
        Sofia.with(getActivity())
                .invasionStatusBar()
                .statusBarBackgroundAlpha(0);
//                .statusBarLightFont();
//                .statusBarDarkFont()

        txtToolbarTitle.setText(Prefs.getStringValue(Constant.PHONE));


        sysAdapter = new BaseQuickAdapter<NewBean, BaseViewHolder>(R.layout.item_find) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, NewBean bean) {
            }


            @Override
            public void onBindViewHolder(@NonNull BaseViewHolder baseViewHolder, int position) {
                GlideUtils.loadImage(baseViewHolder.getView(R.id.ivBg), getItem(position).getCover());
                baseViewHolder.setText(R.id.tvtitle, getItem(position).getName());
                baseViewHolder.setText(R.id.tvTime, TimeUtil.parselongToString(getItem(position).getPushTime()));

            }
        };
        rcv.setAdapter(sysAdapter);
        sysAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                WebviewActivity.intent(getContext(),"发现",sysAdapter.getItem(position).getContentUrl());
            }
        });


        return view;
    }


    @Override
    public void getData() {
        getList();
    }

    public void getList() {
        RetrofitUtil.getInstance().create(NetApi.class, 0)
                .informationList("0").enqueue(new NetCallback<Result<ArrayList<NewBean>>>() {
            @Override
            public void onSuccess(Result<ArrayList<NewBean>> result) {
                if (result.isSuccess()) {
                    sysAdapter.setNewData(result.getData());

                }
            }

            @Override
            public void netLogin() {

            }

            @Override
            public void onError(String errorStr) {
                super.onError(errorStr);
                Log.w("lxl", errorStr);

            }

        });
    }

    /**
     * onDestroyView中进行解绑操作
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    }



    @OnClick({R.id.rlBack, R.id.rlRight})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rlBack:
                break;
            case R.id.rlRight:
                break;
        }
    }
}
